<?php
// +----------------------------------------------------------------------
// | RXThinkCMF_EVTP5.1_PRO前后端分离旗舰版框架 [ RXThinkCMF ]
// +----------------------------------------------------------------------
// | 版权所有 2018~2021 南京RXThinkCMF研发中心
// +----------------------------------------------------------------------
// | 官方网站: http://www.rxthink.cn
// +----------------------------------------------------------------------
// | 作者: ZhangXuanNeng <rxthinkcmf@163.com>
// +----------------------------------------------------------------------
// | 免责声明:
// | 本软件框架禁止任何单位和个人用于任何违法、侵害他人合法利益等恶意的行为，禁止用于任何违
// | 反我国法律法规的一切平台研发，任何单位和个人使用本软件框架用于产品研发而产生的任何意外
// | 、疏忽、合约毁坏、诽谤、版权或知识产权侵犯及其造成的损失 (包括但不限于直接、间接、附带
// | 或衍生的损失等)，本团队不承担任何法律责任。本软件框架只能用于公司和个人内部的法律所允
// | 许的合法合规的软件产品研发，详细声明内容请阅读《框架免责声明》附件；
// +----------------------------------------------------------------------

namespace app\admin\service;


use app\admin\model\BoxAd;
use app\admin\model\BoxAgreement;
use app\admin\model\BoxGame;
use app\admin\model\BoxGameCate;
use app\admin\model\Game;
use app\admin\model\GmApplyGame;
use app\admin\model\Package;
use think\Db;
use think\Exception;
use think\facade\Env;

/**
 * 广告管理-服务类
 * @author ZhangXuanNeng
 * @since: 2023/09/08
 * Class BoxAdService
 * @package app\admin\service
 */
class GmApplyGameService extends BaseService
{
    /**
     * 构造函数
     * LevelService constructor.
     */
    public function __construct()
    {
        $this->model = new GmApplyGame();
    }

    /**
     * 获取数据列表
     * @return array
     * @since 2023/09/08
     * @author ZhangXuanNeng
     */
    public function getList()
    {

        $param = request()->param();
        // 查询条件
        $map = [];
        $data = parent::getList($map);
        foreach ($data['data'] as $key => &$value) {
            $this->model->cacheDelete($value['id']);
            $gameCate = explode(',', $value['game_cate']);
            $gameCateList = (new BoxGameCate())->where(['id' => $gameCate])->select()->toArray();
            $value['game_cate'] = array_column($gameCateList, 'name', 'id');

            $value['game_banner'] = json_decode($value['game_banner'], true);
            $value['game_video'] = json_decode($value['game_video'], true);
        }
        return $data; // TODO: Change the autogenerated stub
    }

    /**
     * 添加或编辑
     * @return array
     * @since 2023/09/08
     * @author ZhangXuanNeng
     */
    public function edit()
    {
        try {
            // 参数
            $data = request()->param();
            $res['is_check'] = $data['is_check'];
            $res['reason'] = $data['reason'];
            $res['status'] = 1;
            $res['id'] = $data['id'];

            Db::startTrans();
            (new GmApplyGame())->edit($res);
            if( $res['is_check'] ==1){
            //获取游戏
            $game = Game::get(['yanfa_game_id' => $data['game_id']]);
            if(!$game){
                throw new Exception('游戏不存在，请先添加游戏');
            }


            //获取游戏审核信息
            $package = (new Package())->where(['game_id' => $game['id'],'sdk_version'=>[1,2]])->select()->toArray();
            if(!$package){
                throw new Exception('游戏包不存在，请先添加游戏包');
            }
            $package = array_column($package,'id','sdk_version');
            $boxGame = (new BoxGame())->where(['is_gm' => $data['game_id']])->find();
            if($boxGame){
                $games['id'] = $boxGame['id'];
            }
            $games['package_id'] = isset($package[1])?$package[1]:'';
            $games['ios_package_id'] = isset($package[2])?$package[2]:'';
            $games['game_name'] = $data['game_name'];
            $games['icon'] = $data['game_icon'];
            $games['cate_id'] = implode(',',array_keys($data['game_cate']));
            $games['content'] = $data['game_desc'];
            $games['version'] = $data['version'];
            $games['game_info'] = $data['game_info'];
            $banner = $this->banner($data['game_banner'], $data['game_video']);
            $games['game_banner'] = json_encode($banner, true);
            $games['status'] = 1;
            $games['is_gm'] = $data['game_id'];
            (new BoxGame())->edit($games);
            }
            //回传数据
            $this->callBackGmCheck($data);
            Db::commit();
            return message();
        }catch (\Exception $e) {
            $this->model->cacheDelete($data['id']);
            Db::rollback();
            return message($e->getMessage(),false);

        }

    }


    public function callBackGmCheck($data){
        $res['game_id'] = $data['game_id'];
        $res['is_check'] = $data['is_check'];
        if($res['is_check'] == 2){
            $res['remark'] = $data['reason'];
        }
        $res['status'] =1;
        $res['sign'] = $this->createSdkSign($res);
//        print_r($res);die;
        $url = Env::get('domain.gm_url').'/GameBoxApi/RsyncGameAttr';
        $result =  json_decode(curl_post($url,$res),true);

        write_log('info','GmApplyGameService.php',json_encode($result),'gmapply',true);

        if($result['code'] !=0){
            throw new Exception($result['msg']);
        }
    }



    public  function createSdkSign($params)
    {
        $secret = 'KfMTQ8EfqzFmv8L1K1TaxNds42yy4r3P';
        ksort($params);
        $str = http_build_query($params).$secret;
        return strtolower(md5($str));
    }


    public function banner($game_banner, $game_video)
    {
        $data = [];
        foreach ($game_banner as $value) {
           $parts = explode('.',$value);
           $res['type'] = end($parts);
           $res['data'] = $value;
           $res['img'] = '';
           $data[] = $res;
        }
        $res['data'] = $game_video[1];
        $res['img'] = $game_video[0];
        $res['type'] = 'mp4';
        $data[] = $res;

      return $data;


    }


}
